# -*- coding: UTF-8 -*-
import pygame
from pygame.locals import *

class Perso:
	def __init__(self, pos):
		self.posx, self.posy = pos
		self.oldx, self.oldy = pos
		self.screenposx, self.screenposy = (0,0)
		self.gridx, self.gridy = 0,0
		self.mainimage = pygame.image.load("Ressources/Graphs/Sprites/link_walking.png").convert_alpha()
		self.dir = 0
		self.frame = 0
		self.framenumber = 5
		self.delayframe = 0
	def update(self, dir, collisionmap, server, scrollx, scrolly):
		self.screenposx = self.posx + scrollx - 50
		self.screenposy = self.posy + scrolly - 50
		self.gridx = (self.posx)/32
		self.gridy = (self.posy)/32
		self.delayframe += 1
		if self.delayframe > 1:
			self.frame += 1
			self.delayframe = 0
		if self.frame > self.framenumber:
			self.frame = 0
		if dir == [0,-1]:
			self.dir = 0
		elif dir == [0,1]:
			self.dir = 3
		elif dir == [-1,0]:
			self.dir = 1
		elif dir == [1,0]:
			self.dir = 2
		self.subimage = self.mainimage.subsurface((100*self.frame,100*self.dir,100,100))
		#screen.blit(subimage, (self.screenposx, self.screenposy))
		
		
		if dir != [0,0]:
			gridx = (self.posx+(16*dir[0]))/32
			gridy = (self.posy+(12*dir[1]))/32
			if collisionmap[gridy][gridx] == True:
				self.posx += dir[0]*7
				self.posy += dir[1]*7
				
		if self.oldx != self.posx or self.oldy != self.posy:
			self.oldx, self.oldy = self.posx, self.posy
			server.sendData("3 " +str(self.posx)+";"+str(self.posy) + "\n")
			
